package com.zhx.basis.设计模式.责任链.demo1;


import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.Map;

public abstract class AuthService {

    private AuthService next;
    private Object o=new Object();
    private Map<String, Object> link01 = new HashMap<>();

    public void queryAuthInfo() {
        //实体类映射
        RebateInfo rebateInfo = entityToMap(o);

        //处理逻辑
//        String status = rebateInfo.getStatus();
//        String userId = rebateInfo.getUserId();
//        System.out.println("消息通知业务" + status);
//        System.out.println("任务中心入库业务" + userId);

        //下一级调用链
        if (next != null) {
            next.queryAuthInfo();
        }

    }


    public Object getO() {
        return o;
    }

    public void setO(Object o) {
        this.o = o;
    }

    /**
     * 字段映射规则，子类实现
     */
    public Map<String, Object> fieldOrm() {
        return link01;
    }

    public void addStatus(Object o) {
        link01.put("status", o);
    }

    public void addUserId(Object o) {
        link01.put("userId", o);
    }

    /**
     * 原始实体类转为指定实体类
     *
     * @param object
     */
    public RebateInfo entityToMap(Object object) {
        Map map = JSON.parseObject(JSON.toJSONString(object), Map.class);
        RebateInfo filter = filter(map, fieldOrm());
        System.out.println("适配器后：" + JSON.toJSONString(filter));
        return filter;
    }


    /**
     * 处理字段不对应的问题
     *
     * @param obj  原始
     * @param link 字段映射规则
     * @return
     */

    public static RebateInfo filter(Map obj, Map<String, Object> link) {
        RebateInfo rebateInfo = new RebateInfo();
        for (String key : link.keySet()) {
            Object val = obj.get(link.get(key));
            try {
                RebateInfo.class.getMethod("set" + key.substring(0, 1).toUpperCase() + key.substring(1), String.class).invoke(rebateInfo, String.valueOf(val));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return rebateInfo;
    }


    public AuthService next() {
        return next;
    }

    public AuthService appendNext(AuthService next) {
        this.next = next;
        return this;
    }

}


class RebateInfo {

    private String userId;  // 用户ID
    private String bizId;   // 业务ID
    private String status;   //
    private String desc;    // 业务描述

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getBizId() {
        return bizId;
    }

    public void setBizId(String bizId) {
        this.bizId = bizId;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }


//    /**
//     * 规定字段映射规则
//     */
//
//    public static void main(String[] args) {
////        字段映射规则
//        Map<String, Object> link01 = new HashMap<>();
//        link01.put("userId", "FILE_ID");
//        link01.put("bizId", "FILE_LINK");
//
////        原始实体类转为map
//        QuestionFileVo deviceAccountVo=new QuestionFileVo().setFILE_ID("1").setFILE_LINK("http://2323232");
//        Map map = JSON.parseObject(JSON.toJSONString(deviceAccountVo), Map.class);
//        System.out.println("适配器后："+JSON.toJSONString(filter(map,link01)));
//    }

}
